<?php

class Load {

	private $load;
	private $controller;
	private $action;
	private $subActions;
	
	public function __construct()
	{
		$this->setVars();
		$this->load = new DefaultController();
		$this->setController();
		$this->setInstance();
		$this->loadPage();

	}

	private function setVars()
	{
		$this->controller = UrlVars::getController();
		$this->action = UrlVars::getAction();
		$this->subActions = UrlVars::getSubActions();
	}

	private function setController()
	{
		if ( !$this->controller )
		{
			$this->load->controller( $GLOBALS['CFG']->config['defaultController'] );
			$this->controller = $GLOBALS['CFG']->config['defaultController'];
		}
		else
		{
			$this->load->controller( $this->controller );
		}
	}

	private function setInstance()
	{
		$this->controller = ucfirst( $this->controller );
		$this->controller = new $this->controller();
		$this->controller->getChildrenLoadInstance();
	}

	private function loadPage()
	{
		$controller = $this->controller;
		$action = $this->action;

		if ( $action )
			try {
				if ( method_exists( $controller , $action) )
					$controller->$action();
				else
					throw new Exception();
			} catch (Exception $e) {
				if ( $GLOBALS['CFG']->config['callControllerOnMissAction'] )
				{
					$this->action = $GLOBALS['CFG']->config['defaultAction'];
					$this->loadPage();
				}
				else
				{
					$this->load->controller('404');
				}
			}
		else
		{
			$action = $GLOBALS['CFG']->config['defaultAction'];
			$controller->$action();
		}
	}

}

?>